<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateUsersTable extends Migrator
{
    /**
     * 创建用户表
     */
    public function up()
    {
        $table = $this->table('users', [
            'id' => false,
            'primary_key' => ['id'],
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_unicode_ci',
            'comment' => '用户表'
        ]);
        
        $table->addColumn('id', 'integer', [
                'identity' => true,
                'signed' => false,
                'comment' => '用户ID'
            ])
            ->addColumn('email', 'string', [
                'limit' => 255,
                'null' => false,
                'comment' => '用户邮箱'
            ])
            ->addColumn('password', 'string', [
                'limit' => 255,
                'null' => false,
                'comment' => '用户密码'
            ])
            ->addColumn('nickname', 'string', [
                'limit' => 500,
                'null' => true,
                'comment' => '用户昵称'
            ])
            ->addColumn('token', 'string', [
                'limit' => 500,
                'null' => true,
                'comment' => '用户token'
            ])
            ->addColumn('avatar', 'string', [
                'limit' => 500,
                'null' => true,
                'comment' => '用户头像URL'
            ])
            ->addColumn('balance', 'decimal', [
                'precision' => 10,
                'scale' => 2,
                'default' => 0.00,
                'null' => false,
                'comment' => '用户余额'
            ])
            ->addColumn('is_vip', 'boolean', [
                'default' => false,
                'null' => false,
                'comment' => '是否VIP用户'
            ])
            ->addColumn('latitude', 'decimal', [
                'precision' => 10,
                'scale' => 8,
                'null' => true,
                'comment' => '纬度'
            ])
            ->addColumn('longitude', 'decimal', [
                'precision' => 11,
                'scale' => 8,
                'null' => true,
                'comment' => '经度'
            ])
            ->addColumn('created_at', 'datetime', [
                'null' => false,
                'comment' => '创建时间'
            ])
            ->addColumn('updated_at', 'datetime', [
                'null' => false,
                'comment' => '更新时间'
            ])
            ->addColumn('apple_user_id', 'string', [
                'limit' => 255,
                'null' => true,
                'comment' => 'Apple登录用户ID',
                'after' => 'email'  // 在email字段后面添加
            ])
            ->addColumn('vip_expired_at', 'datetime', [
                'null' => true,
                'comment' => 'VIP到期时间',
                'after' => 'is_vip'  // 在is_vip字段后面添加
            ])
            ->addIndex(['apple_user_id'], [
                'unique' => true, 
                'name' => 'idx_users_apple_user_id_unique'
            ])
            ->addIndex(['email'], ['unique' => true, 'name' => 'idx_users_email_unique'])
            ->addIndex(['token'], ['name' => 'idx_users_token'])
            ->addIndex(['is_vip'], ['name' => 'idx_users_is_vip'])
            ->addIndex(['latitude', 'longitude'], ['name' => 'idx_users_location'])
            ->create();
    }

    /**
     * 删除用户表
     */
    public function down()
    {
        $this->table('users')->drop()->save();
    }
}